﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormFilterTheSelected : System.Windows.Forms.Form
	{
		public FormFilterTheSelected(Autodesk.Revit.DB.Document Doc, Selection Sel)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = Doc;
			this.sel = Sel;
			base.Icon = Resource.YArchitechIcon;
			base.CenterToScreen();
			this.InitCheckListBox(Doc, Sel);
			string path = Product.DataLocation + "\\SelConfig.xml";
			this.xmlDoc = XMLOperating.LoadXml(path);
			XmlNode xmlNodeByAttribute = XMLOperating.GetXmlNodeByAttribute(this.xmlDoc, "SelSets", "Name", "公式");
			XmlElement[] array = XMLOperating.GetAllElements(this.xmlDoc, xmlNodeByAttribute).ToArray();
			if (array != null)
			{
				this.str1 = array[0].GetAttribute("Current");
				this.str2 = array[1].GetAttribute("Current");
			}
		}

		private void InitCheckListBox(Autodesk.Revit.DB.Document Doc, Selection Sel)
		{
			this.list = this.GetCurrentAllElementType(Doc, Sel);
			foreach (string item in this.GetElementCategray(this.list))
			{
				this.clbChoose.Items.Add(item);
				this.cbCategray.Items.Add(item);
			}
			this.categrayName = this.cbCategray.Text;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.GetDadaFromForm(this.categrayName);
			this.GetTheFilterdConditions(this.sel);
			base.Close();
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public List<Element> GetCurrentAllElementType(Autodesk.Revit.DB.Document Doc, Selection sel)
		{
			this.list = new List<Element>();
			foreach (ElementId elementId in sel.GetElementIds())
			{
				this.list.Add(Doc.GetElementById(elementId));
			}
			return this.list;
		}

		public List<string> GetElementCategray(List<Element> list)
		{
			List<string> list2 = new List<string>();
			foreach (Element element in list)
			{
				list2.Add(element.Category.Name);
			}
			return list2.Distinct<string>().ToList<string>();
		}

		private List<Element> GetTheFilterdConditions(Selection Sel)
		{
			List<Element> list = new List<Element>();
			if (this.clbChoose.CheckedItems != null)
			{
				foreach (object obj in this.clbChoose.CheckedItems)
				{
					string b = (string)obj;
					List<Element> list2 = new List<Element>();
					List<Element> listlast = new List<Element>();
					foreach (ElementId elementId in Sel.GetElementIds())
					{
						if (this.doc.GetElementById(elementId).Category.Name == b)
						{
							list2.Add(this.doc.GetElementById(elementId));
						}
					}
					if (this.cplist != null)
					{
						foreach (CategrayPara categrayPara in this.cplist)
						{
							if (categrayPara.Name == b)
							{
								string[] para = new string[]
								{
									categrayPara.Paras[0].Name,
									categrayPara.Paras[1].Name,
									categrayPara.Paras[2].Name,
									categrayPara.Paras[3].Name
								};
								string[] formula = new string[]
								{
									categrayPara.Paras[0].Formula,
									categrayPara.Paras[1].Formula,
									categrayPara.Paras[2].Formula,
									categrayPara.Paras[3].Formula
								};
								string[] condition = new string[]
								{
									categrayPara.Paras[0].Condition,
									categrayPara.Paras[1].Condition,
									categrayPara.Paras[2].Condition,
									categrayPara.Paras[3].Condition
								};
								list2 = this.DoParameterFilter(para, formula, condition, list2, listlast, categrayPara.Tags);
								break;
							}
						}
					}
					list.AddRange(list2);
				}
				RevitVersionFuncs.ClearSelection(this.sel);
				List<Element> list3 = new List<Element>();
				foreach (Element item in list)
				{
					list3.Add(item);
				}
				RevitVersionFuncs.SetSelection(this.sel, list3);
			}
			return list;
		}

		private void clbChoose_SelectedValueChanged(object sender, EventArgs e)
		{
			if (this.clbChoose.SelectedItem != null)
			{
				string selectedItem = this.clbChoose.SelectedItem.ToString();
				this.cbCategray.SelectedItem = selectedItem;
			}
		}

		private List<Element> DoParameterFilter(string[] para, string[] formula, string[] condition, List<Element> list, List<Element> listlast, int[] tag)
		{
			List<Element> list2 = new List<Element>();
			list2 = this.DoOneParameterFilter(para[0], formula[0], condition[0], list);
			List<Element> list3 = new List<Element>();
			list3 = this.DoOneParameterFilter(para[1], formula[1], condition[1], list);
			List<Element> list4 = new List<Element>();
			list4 = this.DoOneParameterFilter(para[2], formula[2], condition[2], list);
			List<Element> list5 = new List<Element>();
			list5 = this.DoOneParameterFilter(para[3], formula[3], condition[3], list);
			listlast = this.UnionOrIntersect(list2, list3, tag[0]);
			listlast = this.UnionOrIntersect(listlast, list4, tag[1]);
			listlast = this.UnionOrIntersect(listlast, list5, tag[2]);
			return listlast;
		}

		private List<Element> UnionOrIntersect(List<Element> list1, List<Element> list2, int tag)
		{
			List<Element> result = new List<Element>();
			if (tag == 0)
			{
				result = list1.Union(list2).ToList<Element>();
			}
			else if (tag == 1)
			{
				result = list1.Intersect(list2).ToList<Element>();
			}
			return result;
		}

		private List<Element> DoOneParameterFilter(string para, string formula, string condition, List<Element> list)
		{
			List<Element> list2 = new List<Element>();
			if (condition != "" && para != "" && formula != "")
			{
				if (!(formula == "等于"))
				{
					if (!(formula == "大于"))
					{
						if (!(formula == "小于"))
						{
							if (!(formula == "不等于"))
							{
								if (!(formula == "包含"))
								{
									if (formula == "包含于")
									{
										this.FormulaIsBaohanYu(para, condition, list, list2);
									}
								}
								else
								{
									this.FormulaIsBaohan(para, condition, list, list2);
								}
							}
							else
							{
								this.FormulaIsBuDengYU(para, condition, list, list2);
							}
						}
						else
						{
							this.FormulaIsXiaoYU(para, condition, list, list2);
						}
					}
					else
					{
						this.FormulaIsDaYU(para, condition, list, list2);
					}
				}
				else
				{
					this.FormulaIsDengYU(para, condition, list, list2);
				}
			}
			else
			{
				list2 = list;
			}
			return list2;
		}

		private void FormulaIsDengYU(string para, string condition, List<Element> list, List<Element> listlast)
		{
			foreach (Element element in list)
			{
				StorageType storageType;
				if (para == "族名称")
				{
                    storageType = (StorageType)3;
				}
				else
				{
					if (element.GetParameter(para) == null)
					{
						continue;
					}
					storageType = element.GetParameter(para).StorageType;
				}
                if (storageType == (StorageType)2)
				{
					if ((int)(element.GetParameter(para).AsDouble() * 304.8) == int.Parse(condition))
					{
						listlast.Add(element);
					}
				}
                else if (storageType == (StorageType)3)
				{
					string a = "";
					if (para == "族名称")
					{
						a = (element as FamilyInstance).Symbol.Family.Name;
					}
					else if (element.GetParameter(para) != null)
					{
						a = element.GetParameter(para).AsString();
					}
					if (a == condition)
					{
						listlast.Add(element);
					}
				}
                else if (storageType == (StorageType)4)
				{
					if (element.GetParameter(para).AsValueString() == condition)
					{
						listlast.Add(element);
					}
				}
			}
		}

		private void FormulaIsDaYU(string para, string condition, List<Element> list, List<Element> listlast)
		{
			foreach (Element element in list)
			{
				if ((int)element.GetParameter(para).StorageType == 2 && element.GetParameter(para).AsDouble() * 304.8 > double.Parse(condition))
				{
					listlast.Add(element);
				}
			}
		}

		private void FormulaIsXiaoYU(string para, string condition, List<Element> list, List<Element> listlast)
		{
			foreach (Element element in list)
			{
				if ((int)element.GetParameter(para).StorageType == 2 && element.GetParameter(para).AsDouble() * 304.8 < double.Parse(condition))
				{
					listlast.Add(element);
				}
			}
		}

		private void FormulaIsBuDengYU(string para, string condition, List<Element> list, List<Element> listlast)
		{
			foreach (Element element in list)
			{
				StorageType storageType;
				if (para == "族名称")
				{
                    storageType = (StorageType)3;
				}
				else
				{
					if (element.GetParameter(para) == null)
					{
						continue;
					}
					storageType = element.GetParameter(para).StorageType;
				}
                if (storageType == (StorageType)2)
				{
					if (element.GetParameter(para).AsDouble() * 304.8 != double.Parse(condition))
					{
						listlast.Add(element);
					}
				}
                else if (storageType == (StorageType)3)
				{
					string a = "";
					if (para == "族名称")
					{
						a = (element as FamilyInstance).Symbol.Family.Name;
					}
					else if (element.GetParameter(para) != null)
					{
						a = element.GetParameter(para).AsString();
					}
					if (a != condition)
					{
						listlast.Add(element);
					}
				}
                else if (storageType == (StorageType)4)
				{
					if (element.GetParameter(para).AsValueString() != condition)
					{
						listlast.Add(element);
					}
				}
			}
		}

		private void FormulaIsBaohan(string para, string condition, List<Element> list, List<Element> listlast)
		{
			foreach (Element element in list)
			{
				StorageType storageType;
				if (para == "族名称")
				{
                    storageType = (StorageType)3;
				}
				else
				{
					if (element.GetParameter(para) == null)
					{
						continue;
					}
					storageType = element.GetParameter(para).StorageType;
				}
                if (storageType == (StorageType)3)
				{
					string text = "";
					if (para == "族名称")
					{
						text = (element as FamilyInstance).Symbol.Family.Name;
					}
					else if (element.GetParameter(para) != null)
					{
						text = element.GetParameter(para).AsString();
					}
					if (text.Contains(condition))
					{
						listlast.Add(element);
					}
				}
                else if (storageType == (StorageType)4)
				{
					string text2 = "";
					if (element.GetParameter(para) != null)
					{
						text2 = element.GetParameter(para).AsValueString();
					}
					if (text2.Contains(condition))
					{
						listlast.Add(element);
					}
				}
			}
		}

		private void FormulaIsBaohanYu(string para, string condition, List<Element> list, List<Element> listlast)
		{
			foreach (Element element in list)
			{
				StorageType storageType;
				if (para == "族名称")
				{
                    storageType = (StorageType)3;
				}
				else
				{
					if (element.GetParameter(para) == null)
					{
						continue;
					}
					storageType = element.GetParameter(para).StorageType;
				}
                if (storageType == (StorageType)3)
				{
					string value = "";
					if (para == "族名称")
					{
						value = (element as FamilyInstance).Symbol.Family.Name;
					}
					else if (element.GetParameter(para) != null)
					{
						value = element.GetParameter(para).AsString();
					}
					if (condition.Contains(value))
					{
						listlast.Add(element);
					}
				}
                else if (storageType == (StorageType)4)
				{
					string value2 = "";
					if (element.GetParameter(para) != null)
					{
						value2 = element.GetParameter(para).AsValueString();
					}
					if (condition.Contains(value2))
					{
						listlast.Add(element);
					}
				}
			}
		}

		private void ClearAllTheControl()
		{
			this.rbHuo1.Checked = false;
			this.rbHuo2.Checked = false;
			this.rbHuo3.Checked = false;
			this.rbYu1.Checked = true;
			this.rbYu2.Checked = true;
			this.rbYu3.Checked = true;
			this.cbPara1.Items.Clear();
			this.cbPara2.Items.Clear();
			this.cbPara3.Items.Clear();
			this.cbPara4.Items.Clear();
			this.cbPara1.Text = "";
			this.cbPara2.Text = "";
			this.cbPara3.Text = "";
			this.cbPara4.Text = "";
			this.cbFormula1.Items.Clear();
			this.cbFormula2.Items.Clear();
			this.cbFormula3.Items.Clear();
			this.cbFormula4.Items.Clear();
			this.cbFormula1.Text = "";
			this.cbFormula2.Text = "";
			this.cbFormula3.Text = "";
			this.cbFormula4.Text = "";
			this.cbCondition1.Items.Clear();
			this.cbCondition2.Items.Clear();
			this.cbCondition3.Items.Clear();
			this.cbCondition4.Items.Clear();
			this.cbCondition1.Text = "";
			this.cbCondition2.Text = "";
			this.cbCondition3.Text = "";
			this.cbCondition4.Text = "";
		}

		private void rbYu1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbYu1.Checked)
			{
				this.rbHuo1.Checked = false;
			}
		}

		private void rbHuo1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbHuo1.Checked)
			{
				this.rbYu1.Checked = false;
			}
		}

		private void rbYu2_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbYu2.Checked)
			{
				this.rbHuo2.Checked = false;
			}
		}

		private void rbHuo2_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbHuo2.Checked)
			{
				this.rbYu2.Checked = false;
			}
		}

		private void rbYu3_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbYu3.Checked)
			{
				this.rbHuo3.Checked = false;
			}
		}

		private void rbHuo3_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbHuo3.Checked)
			{
				this.rbYu3.Checked = false;
			}
		}

		public void GetDadaFromForm(string name)
		{
			if (name != "")
			{
				Para[] array = new Para[4];
				Para para = new Para(this.cbPara1.Text, this.cbFormula1.Text, this.cbCondition1.Text);
				array[0] = para;
				Para para2 = new Para(this.cbPara2.Text, this.cbFormula2.Text, this.cbCondition2.Text);
				array[1] = para2;
				Para para3 = new Para(this.cbPara3.Text, this.cbFormula3.Text, this.cbCondition3.Text);
				array[2] = para3;
				Para para4 = new Para(this.cbPara4.Text, this.cbFormula4.Text, this.cbCondition4.Text);
				array[3] = para4;
				int[] array2 = new int[3];
				if (this.rbHuo1.Checked)
				{
					array2[0] = 0;
				}
				else
				{
					array2[0] = 1;
				}
				if (this.rbHuo2.Checked)
				{
					array2[1] = 0;
				}
				else
				{
					array2[1] = 1;
				}
				if (this.rbHuo3.Checked)
				{
					array2[2] = 0;
				}
				else
				{
					array2[2] = 1;
				}
				CategrayPara categrayPara = new CategrayPara();
				categrayPara.Name = name;
				categrayPara.Paras = array;
				categrayPara.Tags = array2;
				foreach (CategrayPara categrayPara2 in this.cplist)
				{
					if (categrayPara2.Name == categrayPara.Name)
					{
						this.cplist.Remove(categrayPara2);
						break;
					}
				}
				this.cplist.Add(categrayPara);
			}
		}

		private void cbCategray_SelectedIndexChanged(object sender, EventArgs e)
		{
			string text = this.cbCategray.SelectedItem.ToString();
			this.GetDadaFromForm(this.categrayName);
			this.clbChoose.SelectedItem = text;
			this.clbChoose.SetItemChecked(this.clbChoose.SelectedIndex, true);
			this.ClearAllTheControl();
			foreach (Para para in this.GetParasFromXMlAndProject(text))
			{
				this.cbPara1.Items.Add(para.Name);
				this.cbPara2.Items.Add(para.Name);
				this.cbPara3.Items.Add(para.Name);
				this.cbPara4.Items.Add(para.Name);
			}
			if (this.cplist != null && this.cplist.Count > 0)
			{
				foreach (CategrayPara categrayPara in this.cplist)
				{
					if (categrayPara.Name == text)
					{
						this.cbPara1.Text = categrayPara.Paras[0].Name;
						this.cbFormula1.Text = categrayPara.Paras[0].Formula;
						this.cbCondition1.Text = categrayPara.Paras[0].Condition;
						this.cbPara2.Text = categrayPara.Paras[1].Name;
						this.cbFormula2.Text = categrayPara.Paras[1].Formula;
						this.cbCondition2.Text = categrayPara.Paras[1].Condition;
						this.cbPara3.Text = categrayPara.Paras[2].Name;
						this.cbFormula3.Text = categrayPara.Paras[2].Formula;
						this.cbCondition3.Text = categrayPara.Paras[2].Condition;
						this.cbPara4.Text = categrayPara.Paras[3].Name;
						this.cbFormula4.Text = categrayPara.Paras[3].Formula;
						this.cbCondition4.Text = categrayPara.Paras[3].Condition;
						break;
					}
				}
			}
			this.categrayName = text;
		}

		private List<Para> GetParasFromXMlAndProject(string value)
		{
			this.paralist = new List<Para>();
			XmlNode xmlNodeByAttribute = XMLOperating.GetXmlNodeByAttribute(this.xmlDoc, "SelSets", "Name", value);
			if (xmlNodeByAttribute != null && this.str1 != "" && this.str2 != "")
			{
				foreach (XmlElement xmlElement in XMLOperating.GetAllElements(this.xmlDoc, xmlNodeByAttribute))
				{
					Para para = new Para();
					if (xmlElement.GetAttribute("Tag") == "0")
					{
						para.IsReadFromXml = true;
						para.Name = xmlElement.GetAttribute("Name");
						para.Formula = xmlElement.GetAttribute("CurrentFormula");
						para.Condition = xmlElement.GetAttribute("CurrentCondition");
						if (xmlElement.GetAttribute("IsDouble") == "false")
						{
							para.Formulas = this.str1.Split(new char[]
							{
								','
							});
						}
						else
						{
							para.Formulas = this.str2.Split(new char[]
							{
								','
							});
						}
						para.Conditions = xmlElement.GetAttribute("Conditions").Split(new char[]
						{
							','
						});
					}
					else if (xmlElement.GetAttribute("Tag") == "1")
					{
						para.IsReadFromXml = false;
						para.Name = xmlElement.GetAttribute("Name");
						para.Formula = xmlElement.GetAttribute("CurrentFormula");
						if (xmlElement.GetAttribute("IsDouble") == "false")
						{
							para.Formulas = this.str1.Split(new char[]
							{
								','
							});
						}
						else
						{
							para.Formulas = this.str2.Split(new char[]
							{
								','
							});
						}
						string attribute = xmlElement.GetAttribute("BuiltInParameter");
						if (attribute != "")
						{
							BuiltInParameter builtInParameter = (BuiltInParameter)Enum.Parse(typeof(BuiltInParameter), attribute, true);
							para.Conditions = this.ParameterValues(value, builtInParameter, this.list, para.Name);
						}
						else
						{
							para.Conditions = this.ParameterValues(value, this.list, para.Name);
						}
						if (para.Conditions.Count<string>() > 0)
						{
							para.Condition = para.Conditions[0];
						}
						else
						{
							para.Condition = "";
						}
					}
					this.paralist.Add(para);
				}
			}
			return this.paralist;
		}

		public void cbParaSelectedValueChanged(System.Windows.Forms.ComboBox cbFormula, System.Windows.Forms.ComboBox cbCondition, System.Windows.Forms.ComboBox cbPara)
		{
			cbFormula.Items.Clear();
			cbCondition.Items.Clear();
			foreach (Para para in this.paralist)
			{
				if (para.Name == cbPara.SelectedItem.ToString())
				{
					foreach (string item in para.Formulas)
					{
						cbFormula.Items.Add(item);
					}
					if (para.IsReadFromXml)
					{
						cbFormula.SelectedItem = para.Formula;
					}
					else
					{
						cbFormula.SelectedIndex = 0;
					}
					if (para.Conditions.Count<string>() > 0)
					{
						foreach (string item2 in para.Conditions)
						{
							cbCondition.Items.Add(item2);
						}
						if (para.IsReadFromXml)
						{
							cbCondition.SelectedItem = para.Condition;
						}
						else
						{
							cbCondition.SelectedIndex = 0;
						}
					}
				}
			}
		}

		private void cbPara1_SelectedValueChanged(object sender, EventArgs e)
		{
			this.cbParaSelectedValueChanged(this.cbFormula1, this.cbCondition1, this.cbPara1);
		}

		private void cbPara2_SelectedValueChanged(object sender, EventArgs e)
		{
			this.cbParaSelectedValueChanged(this.cbFormula2, this.cbCondition2, this.cbPara2);
		}

		private void cbPara3_SelectedValueChanged(object sender, EventArgs e)
		{
			this.cbParaSelectedValueChanged(this.cbFormula3, this.cbCondition3, this.cbPara3);
		}

		private void cbPara4_SelectedValueChanged(object sender, EventArgs e)
		{
			this.cbParaSelectedValueChanged(this.cbFormula4, this.cbCondition4, this.cbPara4);
		}

		public string[] ParameterValues(string category, BuiltInParameter builtInParameter, List<Element> list, string name)
		{
			List<Element> list2 = new List<Element>();
			List<string> list3 = new List<string>();
			foreach (Element element in list)
			{
				if (element.Category.Name == category)
				{
					list2.Add(element);
				}
			}
			if (list2.Count > 0)
			{
				foreach (Element elem in list2)
				{
					Parameter parameter = elem.GetParameter(builtInParameter);
					string text = "";
					if (parameter == null)
					{
						elem.GetParameter(name);
					}
					if (parameter != null)
					{
						if ((int)parameter.StorageType == 2)
						{
							text = ((int)(parameter.AsDouble() * 304.8)).ToString();
						}
						else if ((int)parameter.StorageType == 3)
						{
							text = parameter.AsString();
						}
						else if ((int)parameter.StorageType == 4)
						{
							text = parameter.AsValueString().ToString();
						}
						else if ((int)parameter.StorageType == 1)
						{
							text = parameter.AsInteger().ToString();
						}
					}
					if (text != null && text != "")
					{
						list3.Add(text);
					}
				}
			}
			return list3.Distinct<string>().ToArray<string>();
		}

		public string[] ParameterValues(string category, List<Element> list, string name)
		{
			List<Element> list2 = new List<Element>();
			List<string> list3 = new List<string>();
			foreach (Element element in list)
			{
				if (element.Category.Name == category)
				{
					list2.Add(element);
				}
			}
			if (list2.Count > 0)
			{
				foreach (Element element2 in list2)
				{
					string text = "";
					if (name == "族名称")
					{
						text = (element2 as FamilyInstance).Symbol.Family.Name;
					}
					else
					{
						Parameter parameter = element2.GetParameter(name);
						if (parameter != null)
						{
							if ((int)parameter.StorageType == 2)
							{
								text = ((int)(parameter.AsDouble() * 304.8)).ToString();
							}
							else if ((int)parameter.StorageType == 3)
							{
								text = parameter.AsString();
							}
							else if ((int)parameter.StorageType == 4)
							{
								text = parameter.AsValueString().ToString();
							}
							else if ((int)parameter.StorageType == 1)
							{
								text = parameter.AsInteger().ToString();
							}
						}
					}
					if (text != null && text != "")
					{
						list3.Add(text);
					}
				}
			}
			return list3.Distinct<string>().ToArray<string>();
		}

		public Document doc;

		public Selection sel;

		private List<Element> list;

		private List<Para> paralist = new List<Para>();

		private XmlDocument xmlDoc;

		private string categrayName;

		private List<CategrayPara> cplist = new List<CategrayPara>();

		private string str1 = "";

		private string str2 = "";
	}
}
